/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   FrameworkSetting.cs
 * CreateData   :   2023/9/2 11:18:02
 * UnityVersion :   2021.3.20f1c1
 * Description  :   框架项目设置
************************************************/
#pragma warning disable CS0414

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using Sirenix.OdinInspector;
using Main;
using System.IO;

public class FrameworkSetting
{
    private const string macro = "AB_Local";    // 使用本地AB包加载
    [SerializeField, LabelText("使用本工程加载AB包资源"), OnValueChanged("OnToggleLoadABWay")]
    private bool isLoadLocalAB = false;
    [SerializeField, FolderPath, ReadOnly, LabelText("模拟远程加载AB资源路径"), ShowIf("@this.isLoadLocalAB==false")]
    private string rometeAbPath = Path.GetFullPath(PathConfig.BuildAssetBundlePath);

    public FrameworkSetting()
    {
        Init();
    }
    /// <summary>
    /// 初始化
    /// </summary>
    private void Init()
    {
        string currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
        isLoadLocalAB = currentSymbols.Contains(macro);
    }
    private void OnToggleLoadABWay()    // 更改加载 AB 路线
    {
        string currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
        if (isLoadLocalAB)  // 添加宏
        {
            if (!currentSymbols.Contains(macro))
            {
                currentSymbols += $";{macro}"; // 添加宏定义
                PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, currentSymbols);
            }
        }
        else                // 删除宏
        {
            string pattern = $";?{macro}";  // 正则表达式
            currentSymbols = Regex.Replace(currentSymbols, pattern, "");
            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, currentSymbols);
        }
    }
    [InitializeOnEnterPlayMode]
    private static void OnPlayInit()
    {
        string currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
        if (!currentSymbols.Contains(macro))
        {
            AssetBundlesManager.Instance.ABLocalPath = Path.GetFullPath(PathConfig.BuildAssetBundlePath);
        }
    }
}
